package com.diligent.cloud.sfc.factory.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.diligent.cloud.logistics.container.model.LcmBoxFill;
import com.diligent.cloud.sfc.factory.StationFactory;
import com.diligent.cloud.sfc.model.MatType;
import com.diligent.cloud.sfc.model.detail.OffShelvesDetailInfo;
import com.diligent.cloud.sfc.model.enums.Cwoc;
import com.diligent.cloud.sfc.model.info.OffShelvesInfo;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WipService;
import com.diligent.cloud.sfc.util.ConverterUtil;
import com.diligent.cloud.wip.panel.modle.WipStorageLedger;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.google.common.base.Preconditions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * UnShelveWearStripsStation class
 *
 * @author xf107278
 * @date 4/2/2020
 */
@Component
public class UnShelveWearStripsStation implements StationFactory {
    private ISfcService sfcService;
    private final WoOrderFeignService orderFeignService;
    private WipService wipService;

    public UnShelveWearStripsStation(ISfcService sfcService, WoOrderFeignService orderFeignService, WipService wipService) {
        this.sfcService = sfcService;
        this.orderFeignService = orderFeignService;
        this.wipService = wipService;
    }

    @Override
    public Result getOrderInfo(WoOrder order) {
        OffShelvesDetailInfo offShelvesDetailInfo = new OffShelvesDetailInfo();
        List<OffShelvesInfo> offShelvesInfos = new ArrayList<>();
        OffShelvesInfo offShelvesInfo = new OffShelvesInfo();
        offShelvesInfos.add(offShelvesInfo);
        offShelvesDetailInfo.setOffShelvesInfos(offShelvesInfos);

        List<LcmBoxFill> hc2 = sfcService.getOrderSameLocation(Cwoc.穿条缓冲区.getCwocCode(), order.getId(),MatType.胶条);
        Long sameRubberStripTotalLength = 0L;
        Set<String> sameOrderSet = new HashSet<>();
        for (LcmBoxFill lcmBoxFill : hc2) {
            //如何获得胶条长度 数量*长度?
            String orderLength = getOrderLength(lcmBoxFill.getLcmBoxFillDetail().getRefWoOrder());
            sameRubberStripTotalLength =
                    sameRubberStripTotalLength + Long.valueOf(orderLength);

            sameOrderSet.add(lcmBoxFill.getLcmBoxFillDetail().getRefBoxCode());
        }
        offShelvesDetailInfo.setSameOrder(hc2.size());
        offShelvesDetailInfo.setSameTotalLength(sameRubberStripTotalLength);


        offShelvesDetailInfo.setSurplusTotalLength(getSurplusTotalLength(order));


        return ConverterUtil.ConverterByData(offShelvesDetailInfo, Codes.success.getCode(), Codes.success.getMsg());

    }

    String getOrderLength(String orderId) {
        ResponseEntity<com.diligent.cloud.primary.data.model.Result> orderResult =
                orderFeignService.selectById(Long.valueOf(orderId));
        Preconditions.checkNotNull(orderResult.getBody().getData(),"查询不到此[%s]工单",orderId);
        WoOrder order = (WoOrder) orderResult.getBody().getData();
        return order.getCv4();
    }

    Long getSurplusTotalLength(WoOrder order) {
        String matCode = sfcService.getMatCodeByOrderIdAndSystemId(order.getId(),
                order.getSystemId(), MatType.胶条);
        //order.getWorkCenterCode为车间代码
      //  String batchCode = sfcService.getBatchCodeByOrderId(order.getId(), matCode);
        String batchCode = "123";
        ResponseEntity<ObjectResult> inventoryResult =
                wipService.getInventoryByMatCodeAndCwocCode(matCode, order.getWorkCenterCode(),batchCode, order.getSystemId());

        if (inventoryResult.getBody().getCode() == 0) {
            WipStorageLedger storageLedger = (WipStorageLedger) inventoryResult.getBody().getData();
            // TODO 待获取长度方法
            return 0L;
        } else {
            return 0L;
        }
    }
}
